<?php

namespace App\Modules\Apicrzs\Services\Buy;

use App\Modules\Admin\Entities\Member\UsersModel;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use App\Modules\Admin\Entities\Water\WaterWithUserModel;
use App\Modules\Apicrzs\Entities\UserScoreModel;
use App\Modules\Apicrzs\Services\BaseServices;
use App\Traits\CryptAES;
use Termwind\Html\InheritStyles;

class SpreadDeviceServices extends BaseServices
{
    public function __construct(WaterDeviceModel $model)
    {
        $token = \request()->header('token');
        $user_info = CryptAES::decryptToken($token, 'user_grzq');
        $this->user_id = $user_info['data']['user_id'] ?? "";

    }

    public function recode($code) {

        if (UsersModel::getInstance()->where(['code'=>$code])->first()) {

            return false;

        }

        return true;

    }


    public function getQrcode($param)
    {

        $user_id = $this->user_id;
        $user_info = UsersModel::getInstance()->getUserInfo($user_id);
        if (!$user_info){
            return [];
        }

        if (!$user_info['spread_code']){
            $spread_code = $user_id.generateInvitationCode();
            UsersModel::getInstance()->where(['id'=>$user_info['id']])->update(['spread_time'=>time(),'spread_code'=>$spread_code]);
            $user_info['spread_code']=$spread_code;
        }

        return ['spread_code'=>$user_info['spread_code']];
    }

    /** 邀请 设备列表 - 用户
     * @param $param
     * @return array
     */
    public function getLists($param)
    {
        $user_id = $this->user_id;
        $spread_user_list = UsersModel::getInstance()->where(['spread_uid'=>$user_id])->get(['id','nickname'])->toArray();
        if (!$spread_user_list){
            return [];
        }
        $spread_user_lists = array_column($spread_user_list,'nickname','id');
        $spread_user_ids = array_column($spread_user_list,'id');
        $spread_device_ids = WaterWithUserModel::getInstance()->getUserWithWaterByUserIds($spread_user_ids);
        $spread_device_lists = WaterDeviceModel::getInstance()->getSelectListsByIds(array_keys($spread_device_ids));
        $spread_data=[];
        foreach ($spread_device_lists as &$device_list){
            $water_id  = $device_list['water_id'];
            $quilt_uid = $spread_device_ids[$water_id];
            $nickname = $spread_user_lists[$quilt_uid];
            $spread_data[]=[
                'quilt_uid'=>$quilt_uid,
                'nickname'=>$nickname,
                'water_id'=>$device_list['water_id']??"",
                'water_title'=>$device_list['water_title']??"",
            ];
        }
        return  $spread_data;
    }

    /**
     *  分润 详情
     * @param $param
     * @return array
     */
    public function getUserProfitsLists($param){

        $user_id =$this->user_id;
        $quilt_uid = $param['quilt_uid'];
        $water_id = $param['water_id'];
        $where['spread_uid'] = $user_id;
        $where['user_id'] = $quilt_uid;
        $where['water_id'] = $water_id;
        $model = UserScoreModel::getInstance()->where($where);
        $lists = $model->orderBy('created_time', 'DESC')->select(['id','month','manage_score','created_time'])
            ->paginate($this->getLimit($params['limit'] ?? 10));
        return [
            'current_page' => $lists->currentPage(),//当前
            'per_page'     => $lists->perPage(),//每页 10 条
            'count_page'   => $lists->lastPage(),//
            'total'        => $lists->total(), // 总数
            'data'         => $lists->items(),
        ];
    }


}
